package com.itheima.youdianda.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.mapper.SysUserMapper;
import com.itheima.youdianda.pojo.SysRole;
import com.itheima.youdianda.pojo.SysUser;
import com.itheima.youdianda.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Override  //todo:传参支持模糊查询?phone,status参数值为?
    public R listSysUser(String page, String limit, String loginName, String status, String phone) {
        Page<SysUser> pageInfo = new Page<SysUser>(Convert.toLong(page), Convert.toLong(limit));
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(loginName),SysUser::getLogin_name,loginName);
        wrapper.like(StrUtil.isNotBlank(phone),SysUser::getPhone,phone);
        wrapper.eq(StrUtil.isNotBlank(status),SysUser::getStatus,Integer.parseInt(status));
        pageInfo = page(pageInfo,wrapper);

        return R.success(pageInfo);
    }

    @Override
    public void delete(String id) {
        removeById(id);
    }

    @Override
    public void add(SysUser sysUser) {
        String password = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
        sysUser.setPassword(password);
        sysUser.setCreate_date(LocalDateTime.now());
        save(sysUser);
    }

    @Override
    public R rowInfo(String id) {
        return R.success(getById(id));
    }

    @Override
    public void updateSysRole(SysUser sysUser) {
        sysUser.setUpdate_date(LocalDateTime.now());
        updateById(sysUser);
    }


}
